<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Example: Enrich your data based on geolocation | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'geo-match-enrich-policy-type.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/geo-match-enrich-policy-type.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/geo-match-enrich-policy-type.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/geo-match-enrich-policy-type.html" rel="nofollow" target="_blank">../en/geo-match-enrich-policy-type.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="ingest.html">Ingest node</a></span>
»
<span class="breadcrumb-link"><a href="ingest-enriching-data.html">Enrich your data</a></span>
»
<span class="breadcrumb-node">Example: Enrich your data based on geolocation</span>
</div>
<div class="navheader">
<span class="prev">
<a href="enrich-policy-definition.html">« Enrich policy definition</a>
</span>
<span class="next">
<a href="match-enrich-policy-type.html">Example: Enrich your data based on exact values »</a>
</span>
</div>
<div class="section xpack">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="geo-match-enrich-policy-type"></a>Example: Enrich your data based on geolocation<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/ingest/enrich.asciidoc">edit</a><a class="xpack_tag" href="https://www.elastic.co/subscriptions"></a>
</h2>
</div></div></div>
<p><code class="literal">geo_match</code> <a class="xref" href="ingest-enriching-data.html#enrich-policy">enrich policies</a> match enrich data to incoming
documents based on a geographic location, using a
<a class="xref" href="query-dsl-geo-shape-query.html" title="Geo-shape query"><code class="literal">geo_shape</code> query</a>.</p>
<p>The following example creates a <code class="literal">geo_match</code> enrich policy that adds postal
codes to incoming documents based on a set of coordinates. It then adds the
<code class="literal">geo_match</code> enrich policy to a processor in an ingest pipeline.</p>
<p>Use the <a class="xref" href="indices-create-index.html" title="Create index API">create index API</a> to create a source index
containing at least one <code class="literal">geo_shape</code> field.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /postal_codes
{
    "mappings": {
        "properties": {
            "location": {
                "type": "geo_shape"
            },
            "postal_code": {
                "type": "keyword"
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1068.console"></div>
<p>Use the <a class="xref" href="docs-index_.html" title="Index API">index API</a> to index enrich data to this source index.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /postal_codes/_doc/1?refresh=wait_for
{
    "location": {
        "type": "envelope",
        "coordinates": [[13.0, 53.0], [14.0, 52.0]]
    },
    "postal_code": "96598"
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1069.console"></div>
<p>Use the <a class="xref" href="put-enrich-policy-api.html" title="Put enrich policy API">put enrich policy API</a> to create an enrich
policy with the <code class="literal">geo_match</code> policy type. This policy must include:</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
One or more source indices
</li>
<li class="listitem">
A <code class="literal">match_field</code>,
the <code class="literal">geo_shape</code> field from the source indices used to match incoming documents
</li>
<li class="listitem">
Enrich fields from the source indices you’d like to append to incoming
documents
</li>
</ul>
</div>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /_enrich/policy/postal_policy
{
    "geo_match": {
        "indices": "postal_codes",
        "match_field": "location",
        "enrich_fields": ["location","postal_code"]
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1070.console"></div>
<p>Use the <a class="xref" href="execute-enrich-policy-api.html" title="Execute enrich policy API">execute enrich policy API</a> to create an
enrich index for the policy.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /_enrich/policy/postal_policy/_execute</pre>
</div>
<div class="console_widget" data-snippet="snippets/1071.console"></div>
<p>Use the <a class="xref" href="put-pipeline-api.html" title="Put pipeline API">put pipeline API</a> to create an ingest pipeline. In
the pipeline, add an <a class="xref" href="enrich-processor.html" title="Enrich Processor">enrich processor</a> that includes:</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
Your enrich policy.
</li>
<li class="listitem">
The <code class="literal">field</code> of incoming documents used to match the geo_shape of documents
from the enrich index.
</li>
<li class="listitem">
The <code class="literal">target_field</code> used to store appended enrich data for incoming documents.
This field contains the <code class="literal">match_field</code> and <code class="literal">enrich_fields</code> specified in your
enrich policy.
</li>
<li class="listitem">
The <code class="literal">shape_relation</code>, which indicates how the processor matches geo_shapes in
incoming documents to geo_shapes in documents from the enrich index. See
<a class="xref" href="query-dsl-geo-shape-query.html#_spatial_relations" title="Spatial Relations">Spatial Relations</a> for valid options and more information.
</li>
</ul>
</div>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /_ingest/pipeline/postal_lookup
{
  "description": "Enrich postal codes",
  "processors": [
    {
      "enrich": {
        "policy_name": "postal_policy",
        "field": "geo_location",
        "target_field": "geo_data",
        "shape_relation": "INTERSECTS"
      }
    }
  ]
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1072.console"></div>
<p>Use the ingest pipeline to index a document. The incoming document should
include the <code class="literal">field</code> specified in your enrich processor.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /users/_doc/0?pipeline=postal_lookup
{
    "first_name": "Mardy",
    "last_name": "Brown",
    "geo_location": "POINT (13.5 52.5)"
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1073.console"></div>
<p>To verify the enrich processor matched and appended the appropriate field data,
use the <a class="xref" href="docs-get.html" title="Get API">get API</a> to view the indexed document.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /users/_doc/0</pre>
</div>
<div class="console_widget" data-snippet="snippets/1074.console"></div>
<p>The API returns the following response:</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
  "found": true,
  "_index": "users",
  "_type": "_doc",
  "_id": "0",
  "_version": 1,
  "_seq_no": 55,
  "_primary_term": 1,
  "_source": {
    "geo_data": {
      "location": {
        "type": "envelope",
        "coordinates": [[13.0, 53.0], [14.0, 52.0]]
      },
      "postal_code": "96598"
    },
    "first_name": "Mardy",
    "last_name": "Brown",
    "geo_location": "POINT (13.5 52.5)"
  }
}</pre>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="enrich-policy-definition.html">« Enrich policy definition</a>
</span>
<span class="next">
<a href="match-enrich-policy-type.html">Example: Enrich your data based on exact values »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>